using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;
using NPOI.POIFS.FileSystem;
using S7.Net;

namespace JNPF.Basics.Logic
{
    public class CustomeruserService : DataFactory, CustomeruserIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomeruserEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomeruserEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<CustomeruserEntity> GetInfo(int customerId, int type)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<CustomeruserEntity>(m => m.CustomerId == customerId && m.Type == type);
            }
        }


        /// <summary>
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<List<CustomeruserEntity>> GetCustomeruserByCustomerId(int customerId)
        {
            using (var db = this.Basic)
            {
                return await db.GetListAsync<CustomeruserEntity>(m => m.CustomerId == customerId);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<CustomeruserEntity>> GetPageList(CustomeruserListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomeruserEntity>();
                var queryParam = input.queryJson.ToObject();
                //查询类型
                if (input.type.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Type == input.type);
                }
                //查询用户主见
                if (!string.IsNullOrEmpty(input.userId))
                {
                    queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
                }
                //查询客户ID
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }


                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomeruserEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(CustomeruserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(CustomeruserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }


        /// <summary>
        /// 分配
        /// </summary>
        /// <param name="customerbase"></param>
        /// <param name="oldCustomerusers"></param>
        /// <param name="newCustomerusers"></param>
        /// <returns></returns>
        public async Task allot(CustomerbaseEntity customerbase, List<CustomeruserEntity> oldCustomerusers, List<CustomeruserEntity> newCustomerusers)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();


                await db.DeleteAsync(oldCustomerusers);

                await db.InsertAsync(newCustomerusers);

                await db.UpdateAsync(customerbase);
                await db.CommitTransactionAsync();
            }
        }



        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(CustomeruserEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<CustomeruserEntity>> GetList(CustomeruserListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<CustomeruserEntity>();
                //查询类型
                if (input.type.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Type == input.type);
                }
                //查询用户主见
                if (!string.IsNullOrEmpty(input.userId))
                {
                    queryWhere = queryWhere.And(m => m.UserId.Contains(input.userId));
                }
                //查询客户ID
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }


                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<CustomeruserEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}